﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CMS.ORM;//请先添加引用
namespace CMS.ORM.DAL
{
    /// <summary>
    /// 数据访问类NewsOperater。
    /// </summary>
    public class NewsOperater
    {
        public NewsOperater()
        { }
        #region  成员方法
        /// <summary>
        /// 得到最大ID
        /// </summary>
        /// <returns></returns>
        public decimal GetMaxId()
        {
            return DbHelperSQL.GetMaxIDDecimal("id", "NewsOperater");
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from NewsOperater");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CMS.ORM.Model.NewsOperater model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into NewsOperater(");
            strSql.Append("id,newsID,userID,roleID,objectType,contents,lastUpdateTime)");
            strSql.Append(" values (");
            strSql.Append("@id,@newsID,@userID,@roleID,@objectType,@contents,@lastUpdateTime)");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@newsID", SqlDbType.Decimal,9),
					new SqlParameter("@userID", SqlDbType.NVarChar,50),
					new SqlParameter("@roleID", SqlDbType.NVarChar,50),
					new SqlParameter("@objectType", SqlDbType.NChar,4),
					new SqlParameter("@contents", SqlDbType.NVarChar,20),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.newsID;
            parameters[2].Value = model.userID;
            parameters[3].Value = model.roleID;
            parameters[4].Value = model.objectType;
            parameters[5].Value = model.contents;
            parameters[6].Value = model.lastUpdateTime;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CMS.ORM.Model.NewsOperater model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update NewsOperater set ");
            strSql.Append("newsID=@newsID,");
            strSql.Append("userID=@userID,");
            strSql.Append("roleID=@roleID,");
            strSql.Append("objectType=@objectType,");
            strSql.Append("contents=@contents,");
            strSql.Append("lastUpdateTime=@lastUpdateTime");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal,9),
					new SqlParameter("@newsID", SqlDbType.Decimal,9),
					new SqlParameter("@userID", SqlDbType.NVarChar,50),
					new SqlParameter("@roleID", SqlDbType.NVarChar,50),
					new SqlParameter("@objectType", SqlDbType.NChar,4),
					new SqlParameter("@contents", SqlDbType.NVarChar,20),
					new SqlParameter("@lastUpdateTime", SqlDbType.DateTime)};
            parameters[0].Value = model.id;
            parameters[1].Value = model.newsID;
            parameters[2].Value = model.userID;
            parameters[3].Value = model.roleID;
            parameters[4].Value = model.objectType;
            parameters[5].Value = model.contents;
            parameters[6].Value = model.lastUpdateTime;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from NewsOperater ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS.ORM.Model.NewsOperater GetModel(decimal id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,newsID,userID,roleID,objectType,contents,lastUpdateTime from NewsOperater ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Decimal)};
            parameters[0].Value = id;

            CMS.ORM.Model.NewsOperater model = new CMS.ORM.Model.NewsOperater();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = decimal.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["newsID"].ToString() != "")
                {
                    model.newsID = decimal.Parse(ds.Tables[0].Rows[0]["newsID"].ToString());
                }
                model.userID = ds.Tables[0].Rows[0]["userID"].ToString();
                model.roleID = ds.Tables[0].Rows[0]["roleID"].ToString();
                model.objectType = ds.Tables[0].Rows[0]["objectType"].ToString();
                model.contents = ds.Tables[0].Rows[0]["contents"].ToString();
                if (ds.Tables[0].Rows[0]["lastUpdateTime"].ToString() != "")
                {
                    model.lastUpdateTime = DateTime.Parse(ds.Tables[0].Rows[0]["lastUpdateTime"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,newsID,userID,roleID,objectType,contents,lastUpdateTime ");
            strSql.Append(" FROM NewsOperater ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,newsID,userID,roleID,objectType,contents,lastUpdateTime ");
            strSql.Append(" FROM NewsOperater ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "NewsOperater";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}


